
class Student {
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "name: " +name +" age: "+age;
    }
}
public class Test1 {
    public static void main(String[] args) {
        StringBuilder stringBuilder = new StringBuilder("abcdefg");
        stringBuilder.reverse();
        System.out.println(stringBuilder);
    }
    public static void main14(String[] args) {
        long start = System.currentTimeMillis();//获取当前系统的时间戳
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 10000 ; i++) {
            sb.append(i);
        }
        long end = System.currentTimeMillis();
        System.out.println(end - start);


        start = System.currentTimeMillis();
        StringBuffer sb1 = new StringBuffer();
        for (int i = 0; i < 10000 ; i++) {
            sb1.append(i);
        }
        end = System.currentTimeMillis();
        System.out.println(end - start);

        start = System.currentTimeMillis();
        String s = "";
        for (int i = 0; i < 10000 ; i++) {
           s += i;
        }
        end = System.currentTimeMillis();
        System.out.println(end - start);
    }



    public static void main13(String[] args) {
        StringBuilder stringBuilder = new StringBuilder();
        String s ="hello";
        stringBuilder.append(s);
        stringBuilder.append(" world");
        System.out.println(stringBuilder);

    }
    public static void main12(String[] args) {
        String str = "hello";
        char[] chars = str.toCharArray();
        for (char ch : chars){
            System.out.println(ch);
        }

        String s = String.format("%d-%d-%d",2023,12,12);
        System.out.println(s);
    }
    public static void main11(String[] args) {
        String s1 ="\\abaascjsjaacslc";
        System.out.println(s1);
        String ret = s1.replace('a','u');
        System.out.println(ret);
        String ret1 =s1.replace("\\a","hhh");
        System.out.println(ret1);
        String ret2 =s1.replaceAll("\\a","hhh");//没改成功
        System.out.println(ret2);

        String src = new String("ab43a2c43d");
        System.out.println(src.replaceAll("\\d","f"));//=>abffafcffd.
        System.out.println(src.replaceAll("a","f"));//=>abffafcffd.
        System.out.println(src.replace("\\d","f"));//=>ab43a2c43d　//没改成功
        System.out.println(src.replaceAll("a","f"));//=>fb43fc23d.

    }


    //截取、去空格
    public static void main10(String[] args) {
        String str = "abcdef";
        String ret = str.substring(1,3);//[)
        System.out.println(ret);
        System.out.println("======");
        String str2 = "    abcde fefef  f     ";
        System.out.println(str2);
        String ret2 = str2.trim();//去掉前后的空格
        System.out.println(ret2);
    }

    public static void main9(String[] args) {
        String s = String.valueOf(123); //把整数转化为字符串
        System.out.println(s);
        //序列化   序列化与反序列化
        String s2 =String.valueOf(new Student("张三",11));
        System.out.println(s2);
        System.out.println("-----------------------");

        int a = Integer.parseInt("123");
        int a1 = Integer.valueOf("123");
        System.out.println(a+12);

    }

    //字符串的查找
    public static void main8(String[] args) {
        String s = "helylop";
        char ch = s.charAt(1);//返回下标值上的字符（1->对应的下标值）
        System.out.println(ch);//e
        System.out.println("------------");
        int n = s.indexOf("e");//返回改字符对应的下标位置
        System.out.println(n);
        int n1 =s.indexOf("l",2);
        System.out.println(n1);
        int n2 =s.indexOf("l",4);
        System.out.println(n2);
        int n3 =s.lastIndexOf("l",0);
        System.out.println(n3);
        int n4 =s.lastIndexOf("l",3);
        System.out.println(n4);

        System.out.println("!!!!!!!!!!");
       int s1 = s.indexOf("ly",5);
        System.out.println(s1);

        int s2 = s.lastIndexOf("ly",5);
        System.out.println(s2);
    }
    //转义字符串的分割
    public static void main7(String[] args) {
        String s1 = "197.15.6";
        String[] ret = s1.split("\\.");
        for (String s : ret){
            System.out.println(s);
        }
        System.out.println("------------");
        String s2 ="197\\158\\12";
        String[] ret1 = s2.split("\\\\");
        for (String s : ret1){
            System.out.println(s);
        }
    }
    //普通字符串的分割
    public static void main6(String[] args) {

        //分割多次
        String s2 = "he is a dog";
        String[] ret5 = s2.split(" ",12);//分割次数：12-1
        for (String x2 : ret5) {
            System.out.println(x2);
        }
        System.out.println("!!!!!!!!!!!!");
        String[] ret3 = s2.split(" ",3);//分割次数：3-1

        for (String x2 : ret3) {
            System.out.println(x2);
        }
        System.out.println("~~~~~~~~~~~");
        String[] ret4 = s2.split(" ",2);//分割次数：2-1
        for (String x2 : ret4) {
            System.out.println(x2);
        }

        System.out.println("-------------");



        String s1 = "name=zhangsan&age=18";
        //多处分割
        String[] ret2 = s1.split("&|=");
        for (String x2 : ret2) {
            System.out.println(x2);
        }
        System.out.println("------------");

        //分割一次
        String[] ret = s1.split("&");
        for(String x : ret){
            System.out.println(x);
        }
        System.out.println("^^^^^^^^^");
        System.out.println(ret);

    }
    //字符串大小写转化
    public static void main5(String[] args) {
        String s1 = "abcdefg";
        String ret = s1.toUpperCase();
        System.out.println(ret);
        System.out.println(s1);//s1没有发生改变，说明字符串转化成大写时是产生了新的对象

        String s2 ="ABCDEF";
        System.out.println(s2.toUpperCase());
        System.out.println(s2.toLowerCase());
    }

    //忽略大小写判断相同、比较大小
    public static void main4(String[] args) {
        String s1 = "abcDEFg";
        String s2 = "abcdefg";
        System.out.println("equal:"+s1.equals(s2));
        System.out.println("equal:"+s1.equalsIgnoreCase(s2));

        System.out.println("大小："+s1.compareTo(s2));
        System.out.println("大小："+s1.compareToIgnoreCase(s2));
    }
    //比较字符串大小
    public static void main3(String[] args) {
        String s1 = "hello";
        String s2 = "world";
        System.out.println(s1.compareTo(s2));
        System.out.println("----------");
        String s3 = "hello123";
        String s4 = "hello123";
        System.out.println(s1.compareTo(s3));//相同字符才看长度
        System.out.println(s3.compareTo(s2));//不同的字符不看长度
        System.out.println(s3.compareTo(s4));//返回的是ASCII码的差值
    }
    //判断字符串是否相同
    public static void main2(String[] args) {


        //第二个知识点
        String s3 = "abc";
        String s4 = "abcdef";
        String s5 = s3;

        System.out.println(s5);
        System.out.println(s5.equals(s3));//指向同一对象

        Test1 test = new Test1();
        System.out.println(test);//调用的是object的toString方法
        //第一个知识点
        System.out.println("*************");
        String s1 = "abc";
        String s2 = "abcdef";
        System.out.println(s1 == s2);
        System.out.println(s1.equals(s2));

        System.out.println("-------------");
        String str1 = "abcdef";
        String str2 = "abcdef";
        // 为什么输出的都是true？——>字符串常量池
        System.out.println(str1 == str2);//true ——>字符串常量池
        System.out.println(str1.equals(str2));//true

        System.out.println("-----------");
        System.out.println("-----------");
        String str3 = new String("hello");
        String str4 = new String("hello");
        System.out.println(str3.equals(str4));//true->被String重写了
        System.out.println(str3 == str4);//false

    }
    //字符串的构造
    public static void main1(String[] args) {
        //常量串
        String str = "abcde";
        System.out.println(str);
        //new String 对象
        String str1 = new String("abcde");
        System.out.println(str1);
        //字符数组
        char[] array = {'a','b','c','d'};
        String str2 = new String(array);
        System.out.println(str2);
    }
}
